﻿@using Mapster;
@using OnWork.DataCenter.BLL.Standard;
@using OnWork.DataCenter.BLL.DataCenter;
<ValidateForm Model="@JsonToMqttConfig"
              OnValidSubmit="@OnValidSubmit">

    <div class="row">
        <div class="col-12">
            <Select @bind-Value="@JsonToMqttConfig.MqttID" DisplayText="MQTT服务" placeholder="不可为空，50字以内" Items="@MqttItems" />
        </div>

        <div class="col-12">
            <BootstrapInput PlaceHolder="请输入发送的主题"
                            TValue="string"
                            IsSelectAllTextOnEnter="true"
                            ShowLabel="true"
                            DisplayText="MQTT主题" @bind-Value="@JsonToMqttConfig.Topic" />
        </div>
    </div>
    <div class="form-footer mt-3">
        <DialogCloseButton />
        <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa-solid fa-floppy-disk" Text="保存" />
    </div>
</ValidateForm>


@code {
    /// <summary>
    /// Json转DataTable配置
    /// </summary>
    private OnWork.DataCenter.BLL.Standard.JsonToMqttConfig JsonToMqttConfig = new BLL.Standard.JsonToMqttConfig() { };

    [Parameter]
    public Action? OnClose { get; set; }

    [Parameter]
    public Action<JsonToMqttConfig>? OnSave { get; set; }


    [CascadingParameter(Name = "BodyContext")]
    private object? Context { get; set; }

    [CascadingParameter]
    private Modal? Dialog { get; set; }


  

    /// <summary>
    /// 注入BLL MQTT类型
    /// </summary>
    [Inject]
    private DSMqttBLL dsmqttBLL { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();

        if (Context is OnWork.DataCenter.BLL.Standard.JsonToMqttConfig)
        {
            JsonToMqttConfig = Context as OnWork.DataCenter.BLL.Standard.JsonToMqttConfig;
        }
    }


    /// <summary>
    /// Mqtt 列表项目
    /// </summary>
    private IEnumerable<SelectedItem> MqttItems { get; set; }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            var result = await dsmqttBLL.QueryALLMqttAsync();
            if (result != null)
            {
                MqttItems = result.Select(x => new SelectedItem { Value = x.ID.ToString(), Text = x.Name }).ToList();
                StateHasChanged();
            }
        }

    }

    private async Task OnValidSubmit(EditContext item)
    {
        // do someting like save model into db

        OnSave(JsonToMqttConfig);

        // 关闭弹窗
        // close Dialog
        await Dialog.Close();

        // 显示 Toast 提示弹窗
        //await Toast.Show(new ToastOption() { Title = "保存数据", Content = "保存成功，4 秒后自动关闭" });
    }
}
